package com.zlp.mall.modules.pms.controller;


import com.zlp.mall.common.api.Pager;
import com.zlp.mall.common.api.R;
import com.zlp.mall.modules.pms.dto.req.ProductAttributeReq;
import com.zlp.mall.modules.pms.dto.resp.ProductAttrInfoResp;
import com.zlp.mall.modules.pms.dto.resp.ProductAttributeResp;
import com.zlp.mall.modules.pms.model.ProductAttribute;
import com.zlp.mall.modules.pms.service.ProductAttributeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品属性参数表 前端控制器
 * </p>
 *
 * @author ZouLiPing
 * @since 2021-09-23
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/productAttribute")
@Api(value = "ProductAttributeController", tags = "商品属性参数模块")
public class ProductAttributeController {

    private final ProductAttributeService productAttributeService;


    @ApiOperation("根据商品分类的ID 获取商品属性及属性分类")
    @GetMapping(value = "/attrInfo/{productCategoryId}")
    public R<List<ProductAttrInfoResp>> getAttrInfo(@PathVariable Long productCategoryId) {

        List<ProductAttrInfoResp> productAttrInfoList = productAttributeService.getProductAttrInfo(productCategoryId);
        return R.success(productAttrInfoList);
    }

    @GetMapping(value = "/list/{cid}")
    @ApiOperation("根据分类查询属性列表或参数列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "type", value = "0表示属性，1表示参数", required = true, paramType = "query", dataType = "integer")})
    public R<Pager<ProductAttributeResp>> getList(@PathVariable Long cid,
                                                  @RequestParam(value = "type") Integer type,
                                                  @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                  @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {

        Pager<ProductAttributeResp> pager = productAttributeService.getList(cid, type, pageSize, pageNum);
        return R.success(pager);
    }


    @ApiOperation("添加商品属性信息")
    @PostMapping(value = "/create")
    public R create(@RequestBody ProductAttributeReq productAttributeReq) {

        boolean result = productAttributeService.create(productAttributeReq);
        return R.success(result);
    }

    @ApiOperation("修改商品属性信息")
    @PostMapping(value = "/update/{id}")
    public R update(@PathVariable Long id, @RequestBody ProductAttributeReq productAttributeReq) {

        boolean result = productAttributeService.update(id, productAttributeReq);
        return R.success(result);
    }

    @GetMapping(value = "/{id}")
    @ApiOperation("查询单个商品属性")
    public R<ProductAttributeResp> getItem(@PathVariable Long id) {

        ProductAttributeResp productAttribute = productAttributeService.getItem(id);
        return R.success(productAttribute);
    }

    @ApiOperation("批量删除商品属性")
    @PostMapping(value = "/delete")
    public R delete(@RequestParam("ids") List<Long> ids) {

        boolean result = productAttributeService.delete(ids);
        return R.success(result);
    }

}

